home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / fileiconview / qfileiconview.cpp.z / qfileiconview.cpp
C/C++ Source or Header  |  2002-04-08  |  23KB  |  830 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qfileiconview.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "qfileiconview.h"
  12. #include <qpainter.h>
  13. #include <qstringlist.h>
  14. #include <qpixmap.h>
  15. #include <qmime.h>
  16. #include <qstrlist.h>
  17. #include <qdragobject.h>
  18. #include <qmessagebox.h>
  19. #include <qevent.h>
  20. #include <qpopupmenu.h>
  21. #include <qcursor.h>
  22. #include <qapplication.h>
  23. #include <qwmatrix.h>
  24.  
  25. #include <stdlib.h>
  26.  
  27. static const char * file_icon[]={
  28.     "32 32 17 1",
  29.     "# c #000000",
  30.     "a c #ffffff",
  31.     "j c #808080",
  32.     "n c #a0a0a4",
  33.     "g c #c0c0c0",
  34.     "m c #004000",
  35.     "o c #000000",
  36.     "l c #004040",
  37.     "k c #404000",
  38.     "i c #c0c000",
  39.     "h c #ffff00",
  40.     "b c #ffffc0",
  41.     "e c #ff8000",
  42.     "f c #c05800",
  43.     "c c #ffa858",
  44.     "d c #ffdca8",
  45.     ". c None",
  46.     "................................",
  47.     "................................",
  48.     "................................",
  49.     "................................",
  50.     ".............#....###...........",
  51.     "...###......#a##.#aba##.........",
  52.     "..#cdb#....#aaaa#aaaaaa##.......",
  53.     "..#ecdb#..#aaaa#aaaaaaaba##.....",
  54.     "..#fecdb##aaaa#aaaaaaaaaaab##...",
  55.     "...#fecdb#aaa#aaaaaaabaabaaaa##.",
  56.     "....#fecdb#a#baaaaa#baaaaaabaaa#",
  57.     ".....#fecdb#aaaaab#a##baaaaaaa#.",
  58.     ".....##fecdb#bbba#aaaa##baaab#..",
  59.     "....#bb#fecdb#ba#aaaaaaa##aa#...",
  60.     "...#bbbb#fecdb##aaabaaaaaa##....",
  61.     "..#bbbb#b#fecdb#aaaaaaabaaaa##..",
  62.     ".#bbbb#bbb#fecdg#aaaaaaaaaaaba#.",
  63.     "#hhbb#bbbbb#fegg#iiaaaaaaaaaaaa#",
  64.     "#jhhhklibbbk#ggj#aaiiaaaaaaaaa#j",
  65.     ".#mjhhhkmikab####aaabiiaaaaaa#j.",
  66.     "...##jhhhmaaibbaaiibaaaiiaab#n..",
  67.     ".....##j#baaaiiabaaiibaabaa#n...",
  68.     "......##baibaabiibaaaiiabb#j....",
  69.     "......#bbbbiiaabbiiaaaaabon.....",
  70.     ".....#bbbbbbbiiabbaiiaab#n......",
  71.     ".....#jbbbbbbbbiibaabba#n.......",
  72.     "......##jbbbbbbbbiiaabmj........",
  73.     "........##jbbbbbbbbbb#j.........",
  74.     "..........##nbbbbbbbmj..........",
  75.     "............##jbbbb#j...........",
  76.     "..............#mjj#n............",
  77.     "................##n............."};
  78.  
  79. static const char * folder_icon[]={
  80.     "32 32 11 1",
  81.     "# c #000000",
  82.     "b c #c0c000",
  83.     "d c #585858",
  84.     "a c #ffff00",
  85.     "i c #400000",
  86.     "h c #a0a0a4",
  87.     "e c #000000",
  88.     "c c #ffffff",
  89.     "f c #303030",
  90.     "g c #c0c0c0",
  91.     ". c None",
  92.     "...###..........................",
  93.     "...#aa##........................",
  94.     ".###baaa##......................",
  95.     ".#cde#baaa##....................",
  96.     ".#cccdeebaaa##..##f.............",
  97.     ".#cccccdeebaaa##aaa##...........",
  98.     ".#cccccccdeebaaaaaaaa##.........",
  99.     ".#cccccccccdeebababaaa#.........",
  100.     ".#cccccgcgghhebbbbbbbaa#........",
  101.     ".#ccccccgcgggdebbbbbbba#........",
  102.     ".#cccgcgcgcgghdeebiebbba#.......",
  103.     ".#ccccgcggggggghdeddeeba#.......",
  104.     ".#cgcgcgcggggggggghghdebb#......",
  105.     ".#ccgcggggggggghghghghd#b#......",
  106.     ".#cgcgcggggggggghghghhd#b#......",
  107.     ".#gcggggggggghghghhhhhd#b#......",
  108.     ".#cgcggggggggghghghhhhd#b#......",
  109.     ".#ggggggggghghghhhhhhhdib#......",
  110.     ".#gggggggggghghghhhhhhd#b#......",
  111.     ".#hhggggghghghhhhhhhhhd#b#......",
  112.     ".#ddhhgggghghghhhhhhhhd#b#......",
  113.     "..##ddhhghghhhhhhhhhhhdeb#......",
  114.     "....##ddhhhghhhhhhhhhhd#b#......",
  115.     "......##ddhhhhhhhhhhhhd#b#......",
  116.     "........##ddhhhhhhhhhhd#b#......",
  117.     "..........##ddhhhhhhhhd#b#......",
  118.     "............##ddhhhhhhd#b###....",
  119.     "..............##ddhhhhd#b#####..",
  120.     "................##ddhhd#b######.",
  121.     "..................##dddeb#####..",
  122.     "....................##d#b###....",
  123.     "......................####......"};
  124.  
  125.  
  126. static const char * link_icon[]={
  127.     "32 32 12 1",
  128.     "# c #000000",
  129.     "h c #a0a0a4",
  130.     "b c #c00000",
  131.     "d c #585858",
  132.     "i c #400000",
  133.     "c c #ffffff",
  134.     "e c #000000",
  135.     "g c #c0c0c0",
  136.     "a c #ff0000",
  137.     "f c #303030",
  138.     "n c white",
  139.     ". c None",
  140.     "...###..........................",
  141.     "...#aa##........................",
  142.     ".###baaa##......................",
  143.     ".#cde#baaa##....................",
  144.     ".#cccdeebaaa##..##f.............",
  145.     ".#cccccdeebaaa##aaa##...........",
  146.     ".#cccccccdeebaaaaaaaa##.........",
  147.     ".#cccccccccdeebababaaa#.........",
  148.     ".#cccccgcgghhebbbbbbbaa#........",
  149.     ".#ccccccgcgggdebbbbbbba#........",
  150.     ".#cccgcgcgcgghdeebiebbba#.......",
  151.     ".#ccccgcggggggghdeddeeba#.......",
  152.     ".#cgcgcgcggggggggghghdebb#......",
  153.     ".#ccgcggggggggghghghghd#b#......",
  154.     ".#cgcgcggggggggghghghhd#b#......",
  155.     ".#gcggggggggghghghhhhhd#b#......",
  156.     ".#cgcggggggggghghghhhhd#b#......",
  157.     ".#ggggggggghghghhhhhhhdib#......",
  158.     ".#gggggggggghghghhhhhhd#b#......",
  159.     ".#hhggggghghghhhhhhhhhd#b#......",
  160.     ".#ddhhgggghghghhhhhhhhd#b#......",
  161.     "..##ddhhghghhhhhhhhhhhdeb#......",
  162.     "############hhhhhhhhhhd#b#......",
  163.     "#nnnnnnnnnn#hhhhhhhhhhd#b#......",
  164.     "#nnnnnnnnnn#hhhhhhhhhhd#b#......",
  165.     "#nn#nn#nnnn#ddhhhhhhhhd#b#......",
  166.     "#nn##n##nnn###ddhhhhhhd#b###....",
  167.     "#nnn#####nn#..##ddhhhhd#b#####..",
  168.     "#nnnnn##nnn#....##ddhhd#b######.",
  169.     "#nnnnn#nnnn#......##dddeb#####..",
  170.     "#nnnnnnnnnn#........##d#b###....",
  171.     "############..........####......"};
  172.  
  173. static const char * folder_locked_icon[]={
  174.     "32 32 12 1",
  175.     "# c #000000",
  176.     "g c #808080",
  177.     "h c #c0c0c0",
  178.     "f c #c05800",
  179.     "c c #ffffff",
  180.     "d c #585858",
  181.     "b c #ffa858",
  182.     "a c #ffdca8",
  183.     "e c #000000",
  184.     "i c #a0a0a4",
  185.     "j c #c0c0c0",
  186.     ". c None",
  187.     "...###..........................",
  188.     "...#aa##........................",
  189.     ".###baaa##......................",
  190.     ".#cde#baaa##....................",
  191.     ".#cccdeeba#######...............",
  192.     ".#cccccde##fffff##..............",
  193.     ".#cccccc##fffgggg#..............",
  194.     ".#ccccccc#ffg####a##............",
  195.     ".#ccccchc#ffg#eebbaa##..........",
  196.     ".#ccccccc#ffg#ddeebbba##........",
  197.     ".#ccchccc#ffg#ihddeebbba##......",
  198.     ".#cccccaa#ffg#ihhhddeeba##......",
  199.     ".#chchhbbaafg#ihhhihidebb#......",
  200.     ".#cchccbbbbaa#ihhihihid#b#......",
  201.     ".#chchhbb#bbbaaiihihiid#b#......",
  202.     ".#hchhcbb#fbbbafhiiiiid#b#......",
  203.     ".#chchhbb#ffgbbfihiiiid#b#......",
  204.     ".#hhhhhbb#ffg#bfiiiiiid#b#......",
  205.     ".#hhhhhbbaffg#bfiiiiiid#b#......",
  206.     ".#iihhhjbbaab#bfiiiiiid#b#......",
  207.     ".#ddiihhh#bbbabfiiiiiid#b#......",
  208.     "..##ddiih#ffbbbfiiiiiid#b#......",
  209.     "....##ddi#ffg#biiiiiiid#b#......",
  210.     "......##d#ffg#iiiiiiiid#b#......",
  211.     "........##ffg#iiiiiiiid#b#......",
  212.     ".........#ffg#iiiiiiiid#b#......",
  213.     ".........#ffg#ddiiiiiid#b###....",
  214.     ".........##fg###ddiiiid#b#####..",
  215.     "...........####.##ddiid#b######.",
  216.     "..................##dddeb#####..",
  217.     "....................##d#b###....",
  218.     "......................####......"};
  219.  
  220. static QPixmap *iconFolderLockedLarge = 0;
  221. static QPixmap *iconFolderLarge = 0;
  222. static QPixmap *iconFileLarge = 0;
  223. static QPixmap *iconLinkLarge = 0;
  224. static QPixmap *iconFolderLockedSmall = 0;
  225. static QPixmap *iconFolderSmall = 0;
  226. static QPixmap *iconFileSmall = 0;
  227. static QPixmap *iconLinkSmall = 0;
  228.  
  229. static void cleanup()
  230. {
  231.     delete iconFolderLockedLarge;
  232.     iconFolderLockedLarge = 0;
  233.     delete iconFolderLarge;
  234.     iconFolderLarge = 0;
  235.     delete iconFileLarge;
  236.     iconFileLarge = 0;
  237.     delete iconLinkLarge;
  238.     iconLinkLarge = 0;
  239.     delete iconFolderLockedSmall;
  240.     iconFolderLockedSmall = 0;
  241.     delete iconFolderSmall;
  242.     iconFolderSmall = 0;
  243.     delete iconFileSmall;
  244.     iconFileSmall = 0;
  245.     delete iconLinkSmall;
  246.     iconLinkSmall = 0;
  247. }
  248.  
  249. /*****************************************************************************
  250.  *
  251.  * Class QtFileIconDrag
  252.  *
  253.  *****************************************************************************/
  254.  
  255. QtFileIconDrag::QtFileIconDrag( QWidget * dragSource, const char* name )
  256.     : QIconDrag( dragSource, name )
  257. {
  258. }
  259.  
  260. const char* QtFileIconDrag::format( int i ) const
  261. {
  262.     if ( i == 0 )
  263.     return "application/x-qiconlist";
  264.     else if ( i == 1 )
  265.     return "text/uri-list";
  266.     else
  267.     return 0;
  268. }
  269.  
  270. QByteArray QtFileIconDrag::encodedData( const char* mime ) const
  271. {
  272.     QByteArray a;
  273.     if ( QString( mime ) == "application/x-qiconlist" ) {
  274.     a = QIconDrag::encodedData( mime );
  275.     } else if ( QString( mime ) == "text/uri-list" ) {
  276.     QString s = urls.join( "\r\n" );
  277.     a.resize( s.length() );
  278.     memcpy( a.data(), s.latin1(), s.length() );
  279.     }
  280.     return a;
  281. }
  282.  
  283. bool QtFileIconDrag::canDecode( QMimeSource* e )
  284. {
  285.     return e->provides( "application/x-qiconlist" ) ||
  286.     e->provides( "text/uri-list" );
  287. }
  288.  
  289. void QtFileIconDrag::append( const QIconDragItem &item, const QRect &pr,
  290.                  const QRect &tr, const QString &url )
  291. {
  292.     QIconDrag::append( item, pr, tr );
  293.     urls << url;
  294. }
  295.  
  296. /*****************************************************************************
  297.  *
  298.  * Class QtFileIconViewItem
  299.  *
  300.  *****************************************************************************/
  301.  
  302. QtFileIconViewItem::QtFileIconViewItem( QtFileIconView *parent, QFileInfo *fi )
  303.     : QIconViewItem( parent, fi->fileName() ), itemFileName( fi->filePath() ),
  304.       itemFileInfo( fi ), checkSetText( FALSE )
  305. {
  306.     vm = QtFileIconView::Large;
  307.  
  308.     if ( itemFileInfo->isDir() )
  309.     itemType = Dir;
  310.     else if ( itemFileInfo->isFile() )
  311.     itemType = File;
  312.     if ( itemFileInfo->isSymLink() )
  313.     itemType = Link;
  314.  
  315.     viewModeChanged( ( (QtFileIconView*)iconView() )->viewMode() );
  316.  
  317.     if ( itemFileInfo->fileName() == "." ||
  318.      itemFileInfo->fileName() == ".." )
  319.     setRenameEnabled( FALSE );
  320.  
  321.     checkSetText = TRUE;
  322.  
  323.     QObject::connect( &timer, SIGNAL( timeout() ),
  324.               iconView(), SLOT( openFolder() ) );
  325. }
  326.  
  327. void QtFileIconViewItem::paintItem( QPainter *p, const QColorGroup &cg )
  328. {
  329.     if ( itemFileInfo->isSymLink() ) {
  330.     QFont f( p->font() );
  331.     f.setItalic( TRUE );
  332.     p->setFont( f );
  333.     }
  334.  
  335.     QIconViewItem::paintItem( p, cg );
  336. }
  337.  
  338. void QtFileIconViewItem::viewModeChanged( QtFileIconView::ViewMode m )
  339. {
  340.     vm = m;
  341.     setDropEnabled( itemType == Dir && QDir( itemFileName ).isReadable() );
  342.     calcRect();
  343. }
  344.  
  345. QPixmap *QtFileIconViewItem::pixmap() const
  346. {
  347.     switch ( itemType ) {
  348.     case Dir:
  349.     {
  350.         if ( !QDir( itemFileName ).isReadable() ) {
  351.         if ( vm == QtFileIconView::Small )
  352.             return iconFolderLockedSmall;
  353.         else
  354.             return iconFolderLockedLarge;
  355.         } else {
  356.         if ( vm == QtFileIconView::Small )
  357.             return iconFolderSmall;
  358.         else
  359.             return iconFolderLarge;
  360.         }
  361.     }
  362.     case Link:
  363.     {
  364.         if ( vm == QtFileIconView::Small )
  365.         return iconLinkSmall;
  366.         else
  367.         return iconLinkLarge;
  368.     }
  369.     default:
  370.     {
  371.         if ( vm == QtFileIconView::Small )
  372.         return iconFileSmall;
  373.         else
  374.         return iconFileLarge;
  375.     }
  376.     }
  377. }
  378.  
  379. QtFileIconViewItem::~QtFileIconViewItem()
  380. {
  381.     delete itemFileInfo;
  382. }
  383.  
  384. void QtFileIconViewItem::setText( const QString &text )
  385. {
  386.     if ( checkSetText ) {
  387.     if ( text == "." || text == "." || text.isEmpty() )
  388.         return;
  389.     QDir dir( itemFileInfo->dir() );
  390.     if ( dir.rename( itemFileInfo->fileName(), text ) ) {
  391.         itemFileName = itemFileInfo->dirPath( TRUE ) + "/" + text;
  392.         delete itemFileInfo;
  393.         itemFileInfo = new QFileInfo( itemFileName );
  394.         QIconViewItem::setText( text );
  395.     }
  396.     } else {
  397.     QIconViewItem::setText( text );
  398.     }
  399. }
  400.  
  401. bool QtFileIconViewItem::acceptDrop( const QMimeSource *e ) const
  402. {
  403.     if ( type() == Dir && e->provides( "text/uri-list" ) &&
  404.      dropEnabled() )
  405.     return TRUE;
  406.  
  407.     return FALSE;
  408. }
  409.  
  410. void QtFileIconViewItem::dropped( QDropEvent *e, const QValueList<QIconDragItem> & )
  411. {
  412.     timer.stop();
  413.  
  414.     if ( !QUriDrag::canDecode( e ) ) {
  415.     e->ignore();
  416.     return;
  417.     }
  418.  
  419.     QStrList lst;
  420.     QUriDrag::decode( e, lst );
  421.  
  422.     QString str;
  423.     if ( e->action() == QDropEvent::Copy )
  424.     str = "Copy\n\n";
  425.     else
  426.     str = "Move\n\n";
  427.     for ( uint i = 0; i < lst.count(); ++i )
  428.     str += QString( "   %1\n" ).arg( lst.at( i ) );
  429.     str += QString( "\n"
  430.             "To\n\n"
  431.             "    %1" ).arg( filename() );
  432.  
  433.     QMessageBox::information( iconView(), e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
  434.     if ( e->action() == QDropEvent::Move )
  435.     QMessageBox::information( iconView(), "Remove" , str, "Not Implemented" );
  436.     e->acceptAction();
  437. }
  438.  
  439. void QtFileIconViewItem::dragEntered()
  440. {
  441.     if ( type() != Dir ||
  442.      type() == Dir && !QDir( itemFileName ).isReadable() )
  443.     return;
  444.  
  445.     ( (QtFileIconView*)iconView() )->setOpenItem( this );
  446.     timer.start( 1500 );
  447. }
  448.  
  449. void QtFileIconViewItem::dragLeft()
  450. {
  451.     if ( type() != Dir ||
  452.      type() == Dir && !QDir( itemFileName ).isReadable() )
  453.     return;
  454.  
  455.     timer.stop();
  456. }
  457.  
  458. /*****************************************************************************
  459.  *
  460.  * Class QtFileIconView
  461.  *
  462.  *****************************************************************************/
  463.  
  464. QtFileIconView::QtFileIconView( const QString &dir, QWidget *parent, const char *name )
  465.     : QIconView( parent, name ), viewDir( dir ), newFolderNum( 0 )
  466. {
  467.     if ( !iconFolderLockedLarge ) {
  468.     qAddPostRoutine( cleanup );
  469.     QWMatrix m;
  470.     m.scale( 0.6, 0.6 );
  471.     QPixmap iconpix( folder_locked_icon );
  472.     iconFolderLockedLarge = new QPixmap( folder_locked_icon );
  473.     iconpix = iconpix.xForm( m );
  474.     iconFolderLockedSmall = new QPixmap( iconpix );
  475.     iconpix = QPixmap( folder_icon );
  476.     iconFolderLarge = new QPixmap( folder_icon );
  477.     iconpix = iconpix.xForm( m );
  478.     iconFolderSmall = new QPixmap( iconpix );
  479.     iconpix = QPixmap( file_icon );
  480.     iconFileLarge = new QPixmap( file_icon );
  481.     iconpix = iconpix.xForm( m );
  482.     iconFileSmall = new QPixmap( iconpix );
  483.     iconpix = QPixmap( link_icon );
  484.     iconLinkLarge = new QPixmap( link_icon );
  485.     iconpix = iconpix.xForm( m );
  486.     iconLinkSmall = new QPixmap( iconpix );
  487.     }
  488.  
  489.     vm = Large;
  490.  
  491.     setGridX( 75 );
  492.     setResizeMode( Adjust );
  493.     setWordWrapIconText( FALSE );
  494.  
  495.     connect( this, SIGNAL( doubleClicked( QIconViewItem * ) ),
  496.          this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
  497.     connect( this, SIGNAL( returnPressed( QIconViewItem * ) ),
  498.          this, SLOT( itemDoubleClicked( QIconViewItem * ) ) );
  499.     connect( this, SIGNAL( dropped( QDropEvent *, const QValueList<QIconDragItem> & ) ),
  500.          this, SLOT( slotDropped( QDropEvent *, const QValueList<QIconDragItem> & ) ) );
  501.     connect( this, SIGNAL( contextMenuRequested( QIconViewItem *, const QPoint & ) ),
  502.          this, SLOT( slotRightPressed( QIconViewItem * ) ) );
  503.  
  504.     setHScrollBarMode( AlwaysOff );
  505.     setVScrollBarMode( Auto );
  506.  
  507.     setAutoArrange( TRUE );
  508.     setSorting( TRUE );
  509.     openItem = 0;
  510. }
  511.  
  512. void QtFileIconView::openFolder()
  513. {
  514.     if ( !openItem )
  515.     return;
  516.     if ( openItem->type() != QtFileIconViewItem::Dir ||
  517.      openItem->type() == QtFileIconViewItem::Dir &&
  518.      !QDir( openItem->itemFileName ).isReadable() )
  519.     return;
  520.  
  521.     openItem->timer.stop();
  522.     setDirectory( openItem->itemFileName );
  523. }
  524.  
  525. void QtFileIconView::setDirectory( const QString &dir )
  526. {
  527.     viewDir = QDir( dir );
  528.     readDir( viewDir );
  529. }
  530.  
  531. void QtFileIconView::setDirectory( const QDir &dir )
  532. {
  533.     viewDir = dir;
  534.     readDir( viewDir );
  535. }
  536.  
  537. void QtFileIconView::newDirectory()
  538. {
  539.     setAutoArrange( FALSE );
  540.     selectAll( FALSE );
  541.     if ( viewDir.mkdir( QString( "New Folder %1" ).arg( ++newFolderNum ) ) ) {
  542.     QFileInfo *fi = new QFileInfo( viewDir, QString( "New Folder %1" ).arg( newFolderNum ) );
  543.     QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
  544.     item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
  545.     delete fi;
  546.     repaintContents( contentsX(), contentsY(), contentsWidth(), contentsHeight(), FALSE );
  547.     ensureItemVisible( item );
  548.     item->setSelected( TRUE, TRUE );
  549.     setCurrentItem( item );
  550.     repaintItem( item );
  551.     qApp->processEvents();
  552.     item->rename();
  553.     }
  554.     setAutoArrange( TRUE );
  555. }
  556.  
  557. QDir QtFileIconView::currentDir()
  558. {
  559.     return viewDir;
  560. }
  561.  
  562. static bool isRoot( const QString &s )
  563. {
  564. #if defined(Q_OS_UNIX)
  565.     if ( s == "/" )
  566.     return TRUE;
  567. #elif defined(Q_OS_WIN32)
  568.     QString p = s;
  569.     if ( p.length() == 3 &&
  570.      p.right( 2 ) == ":/" )
  571.     return TRUE;
  572.     if ( p[ 0 ] == '/' && p[ 1 ] == '/' ) {
  573.     int slashes = p.contains( '/' );
  574.     if ( slashes <= 3 )
  575.         return TRUE;
  576.     if ( slashes == 4 && p[ (int)p.length() - 1 ] == '/' )
  577.         return TRUE;
  578.     }
  579. #endif
  580.  
  581.     return FALSE;
  582. }
  583.  
  584. void QtFileIconView::readDir( const QDir &dir )
  585. {
  586.     if ( !dir.isReadable() )
  587.     return;
  588.  
  589.     if ( isRoot( dir.absPath() ) )
  590.     emit disableUp();
  591.     else
  592.     emit enableUp();
  593.  
  594.     clear();
  595.  
  596.     emit directoryChanged( dir.absPath() );
  597.  
  598.     const QFileInfoList *filist = dir.entryInfoList( QDir::DefaultFilter, QDir::DirsFirst | QDir::Name );
  599.  
  600.     emit startReadDir( filist->count() );
  601.  
  602.     QFileInfoListIterator it( *filist );
  603.     QFileInfo *fi;
  604.     bool allowRename = FALSE, allowRenameSet = FALSE;
  605.     while ( ( fi = it.current() ) != 0 ) {
  606.     ++it;
  607.     if ( fi && fi->fileName() == ".." && ( fi->dirPath() == "/" || fi->dirPath().isEmpty() ) )
  608.         continue;
  609.     emit readNextDir();
  610.     QtFileIconViewItem *item = new QtFileIconViewItem( this, new QFileInfo( *fi ) );
  611.     if ( fi->isDir() )
  612.         item->setKey( QString( "000000%1" ).arg( fi->fileName() ) );
  613.     else
  614.         item->setKey( fi->fileName() );
  615.     if ( !allowRenameSet ) {
  616.         if ( !QFileInfo( fi->absFilePath() ).isWritable() ||
  617.          item->text() == "." || item->text() == ".." )
  618.         allowRename = FALSE;
  619.         else
  620.         allowRename = TRUE;
  621.         if ( item->text() == "." || item->text() == ".." )
  622.         allowRenameSet = FALSE;
  623.         else
  624.         allowRenameSet = TRUE;
  625.     }
  626.     item->setRenameEnabled( allowRename );
  627.     }
  628.  
  629.     if ( !QFileInfo( dir.absPath() ).isWritable() )
  630.     emit disableMkdir();
  631.     else
  632.     emit enableMkdir();
  633.  
  634.     emit readDirDone();
  635. }
  636.  
  637. void QtFileIconView::itemDoubleClicked( QIconViewItem *i )
  638. {
  639.     QtFileIconViewItem *item = ( QtFileIconViewItem* )i;
  640.  
  641.     if ( item->type() == QtFileIconViewItem::Dir ) {
  642.     viewDir = QDir( item->filename() );
  643.     readDir( viewDir );
  644.     } else if ( item->type() == QtFileIconViewItem::Link &&
  645.         QFileInfo( QFileInfo( item->filename() ).readLink() ).isDir() ) {
  646.     viewDir = QDir( QFileInfo( item->filename() ).readLink() );
  647.     readDir( viewDir );
  648.     }
  649. }
  650.  
  651. QDragObject *QtFileIconView::dragObject()
  652. {
  653.     if ( !currentItem() )
  654.     return 0;
  655.  
  656.     QPoint orig = viewportToContents( viewport()->mapFromGlobal( QCursor::pos() ) );
  657.     QtFileIconDrag *drag = new QtFileIconDrag( viewport() );
  658.     drag->setPixmap( *currentItem()->pixmap(),
  659.              QPoint( currentItem()->pixmapRect().width() / 2, currentItem()->pixmapRect().height() / 2 ) );
  660.     for ( QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem(); item;
  661.       item = (QtFileIconViewItem*)item->nextItem() ) {
  662.     if ( item->isSelected() ) {
  663.         QIconDragItem id;
  664.         id.setData( QCString( item->filename() ) );
  665.         drag->append( id,
  666.               QRect( item->pixmapRect( FALSE ).x() - orig.x(),
  667.                  item->pixmapRect( FALSE ).y() - orig.y(),
  668.                  item->pixmapRect().width(), item->pixmapRect().height() ),
  669.               QRect( item->textRect( FALSE ).x() - orig.x(),
  670.                  item->textRect( FALSE ).y() - orig.y(),
  671.                  item->textRect().width(), item->textRect().height() ),
  672.               QString( item->filename() ) );
  673.     }
  674.     }
  675.  
  676.     return drag;
  677. }
  678.  
  679. void QtFileIconView::keyPressEvent( QKeyEvent *e )
  680. {
  681.     if ( e->key() == Key_N &&
  682.      ( e->state() & ControlButton ) )
  683.     newDirectory();
  684.     else
  685.     QIconView::keyPressEvent( e );
  686. }
  687.  
  688. void QtFileIconView::slotDropped( QDropEvent *e, const QValueList<QIconDragItem> & )
  689. {
  690.     if ( openItem )
  691.     openItem->timer.stop();
  692.     if ( !QUriDrag::canDecode( e ) ) {
  693.     e->ignore();
  694.     return;
  695.     }
  696.  
  697.     QStrList lst;
  698.     QUriDrag::decode( e, lst );
  699.  
  700.     QString str;
  701.     if ( e->action() == QDropEvent::Copy )
  702.     str = "Copy\n\n";
  703.     else
  704.     str = "Move\n\n";
  705.     for ( uint i = 0; i < lst.count(); ++i )
  706.     str += QString( "   %1\n" ).arg( lst.at( i ) );
  707.     str += QString( "\n"
  708.             "To\n\n"
  709.             "    %1" ).arg( viewDir.absPath() );
  710.  
  711.     QMessageBox::information( this, e->action() == QDropEvent::Copy ? "Copy" : "Move" , str, "Not Implemented" );
  712.     if ( e->action() == QDropEvent::Move )
  713.     QMessageBox::information( this, "Remove" , str, "Not Implemented" );
  714.     e->acceptAction();
  715.     openItem = 0;
  716. }
  717.  
  718. void QtFileIconView::viewLarge()
  719. {
  720.     setViewMode( Large );
  721. }
  722.  
  723. void QtFileIconView::viewSmall()
  724. {
  725.     setViewMode( Small );
  726. }
  727.  
  728. void QtFileIconView::viewBottom()
  729. {
  730.     setItemTextPos( Bottom );
  731. }
  732.  
  733. void QtFileIconView::viewRight()
  734. {
  735.     setItemTextPos( Right );
  736. }
  737.  
  738. void QtFileIconView::flowEast()
  739. {
  740.     setHScrollBarMode( AlwaysOff );
  741.     setVScrollBarMode( Auto );
  742.     setArrangement( LeftToRight );
  743. }
  744.  
  745. void QtFileIconView::flowSouth()
  746. {
  747.     setVScrollBarMode( AlwaysOff );
  748.     setHScrollBarMode( Auto );
  749.     setArrangement( TopToBottom );
  750. }
  751.  
  752. void QtFileIconView::sortAscending()
  753. {
  754.     sort( TRUE );
  755. }
  756.  
  757. void QtFileIconView::sortDescending()
  758. {
  759.     sort( FALSE );
  760. }
  761.  
  762. void QtFileIconView::itemTextTruncate()
  763. {
  764.     setWordWrapIconText( FALSE );
  765. }
  766.  
  767. void QtFileIconView::itemTextWordWrap()
  768. {
  769.     setWordWrapIconText( TRUE );
  770. }
  771.  
  772. void QtFileIconView::slotRightPressed( QIconViewItem *item )
  773. {
  774.     if ( !item ) { // right pressed on viewport
  775.     QPopupMenu menu( this );
  776.  
  777.     menu.insertItem( "&Large view", this, SLOT( viewLarge() ) );
  778.     menu.insertItem( "&Small view", this, SLOT( viewSmall() ) );
  779.     menu.insertSeparator();
  780.     menu.insertItem( "Text at the &bottom", this, SLOT( viewBottom() ) );
  781.     menu.insertItem( "Text at the &right", this, SLOT( viewRight() ) );
  782.     menu.insertSeparator();
  783.     menu.insertItem( "Arrange l&eft to right", this, SLOT( flowEast() ) );
  784.     menu.insertItem( "Arrange t&op to bottom", this, SLOT( flowSouth() ) );
  785.     menu.insertSeparator();
  786.     menu.insertItem( "&Truncate item text", this, SLOT( itemTextTruncate() ) );
  787.     menu.insertItem( "&Wordwrap item text", this, SLOT( itemTextWordWrap() ) );
  788.     menu.insertSeparator();
  789.     menu.insertItem( "Arrange items in &grid", this, SLOT( arrangeItemsInGrid() ) );
  790.     menu.insertSeparator();
  791.     menu.insertItem( "Sort &ascending", this, SLOT( sortAscending() ) );
  792.     menu.insertItem( "Sort &descending", this, SLOT( sortDescending() ) );
  793.  
  794.     menu.setMouseTracking( TRUE );
  795.     menu.exec( QCursor::pos() );
  796.     } else { // on item
  797.     QPopupMenu menu( this );
  798.  
  799.     int RENAME_ITEM = menu.insertItem( "Rename Item" );
  800.     int REMOVE_ITEM = menu.insertItem( "Remove Item" );
  801.  
  802.     menu.setMouseTracking( TRUE );
  803.     int id = menu.exec( QCursor::pos() );
  804.  
  805.     if ( id == -1 )
  806.         return;
  807.  
  808.     if ( id == RENAME_ITEM && item->renameEnabled() ) {
  809.         item->rename();
  810.     } else if ( id == REMOVE_ITEM ) {
  811.         delete item;
  812.         QMessageBox::information( this, "Not implemented!", "Deleting files not implemented yet,\n"
  813.                       "The item has only been removed from the view! " );
  814.     }
  815.     }
  816. }
  817.  
  818. void QtFileIconView::setViewMode( ViewMode m )
  819. {
  820.     if ( m == vm )
  821.     return;
  822.  
  823.     vm = m;
  824.     QtFileIconViewItem *item = (QtFileIconViewItem*)firstItem();
  825.     for ( ; item; item = (QtFileIconViewItem*)item->nextItem() )
  826.     item->viewModeChanged( vm );
  827.  
  828.     arrangeItemsInGrid();
  829. }
  830.